package com.demo.webmagic.pageProcessor;

import org.apache.http.HttpHost;

import com.demo.common.constant.ConstantHttp;
import com.demo.common.model.Proxyip;
import com.demo.webmagic.dto.DtoProxyIp;
import com.demo.webmagic.dto.DtoResult;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class PageProcessorValiProxyIp implements PageProcessor {

	private Proxyip proxyip;

	private Site site = Site.me().setRetryTimes(3);
	Log log = Log.getLog(DtoProxyIp.class);

	public PageProcessorValiProxyIp() {
	}

	public PageProcessorValiProxyIp(Proxyip proxyip) {
		this.proxyip = proxyip;
		HttpHost httpProxy = new HttpHost(proxyip.getIpStr(), Integer.parseInt(proxyip.getPortStr()));
		site.setHttpProxy(httpProxy);
	}

	@Override
	public Site getSite() {
		return site;
	}

	@Override
	public void process(Page page) {
		DtoResult dtoResult = new DtoResult();
		try {
			String resultStr = page.getHtml().xpath("//center/text()").toString().trim();
			resultStr = resultStr.substring(resultStr.indexOf("[") + 1, resultStr.indexOf("]"));
			if(StrKit.notBlank(resultStr) && resultStr.equals(proxyip.getIpStr())) {
				dtoResult.setCode(ConstantHttp.code_success);
				dtoResult.setObj(proxyip);
			} else {
				dtoResult.setCode(ConstantHttp.code_ip_invalid);
				dtoResult.setObj(proxyip);
			}
		} catch (Exception e) {
			dtoResult.setCode(ConstantHttp.code_fail);
			dtoResult.setObj(proxyip);
			log.error(e.getMessage());
		} finally {
			page.putField("result", dtoResult);
		}
	}

	public static void main(String[] args) {
		String url = "http://www.ip138.com/ip2city.asp";
		Proxyip proxyip = new Proxyip();
		proxyip.setIpStr("120.52.73.7");
		proxyip.setPortStr("8080");
		Spider.create(new PageProcessorValiProxyIp(proxyip)).addUrl(url).start();
	}
}
